<template>
  <Card title="医疗机构统计" :loading="loading">
    <!-- <template #title>
      <div style="display: flex;">
        <div style="margin: auto;">
          医疗机构统计
        </div>
       <BasicForm style="flex: 1;" ref="form" @submit="handleSubmit" @reset="handleReset" @register="registerForm" />
      </div>
    </template> -->
    <!-- <BasicForm  ref="form" @submit="handleSubmit" @reset="handleReset" @register="registerForm" /> -->
    <div style="text-align: right;font-size: 26px;">
      总数: {{ props.res.sumQty || 0}}
     </div>
    <div ref="chartRef" :style="{ width, height }"></div>
  </Card>
</template>
<script lang="ts" setup>
  import { Ref, ref, watch } from 'vue';
  import { Card } from 'ant-design-vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  // import { getHospTypeCensus,areaTreeData } from '/@/api/sys/desktop';
  // import { areaTreeData } from '/@/api/sys/area';

  const props = defineProps({
    loading: Boolean,
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '350px',
    },
    res: {
      type: Array,
      default: {
        data: {},
      },
    },
  });

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  watch(
    () => [props.loading, props.res],
    () => {
      if (props.loading) {
        return;
      }
      handleData({});
    },
    { immediate: true },
  );

  // const [registerForm, { getFieldsValue, validate, setProps, setFieldsValue }] =
  //   useForm({
  //     labelWidth: 60,
  //     showActionButtonGroup: true,
  //     showAdvancedButton: true,
  //     // compact: true,
  //     baseColProps: { lg:  24, md: 24 },
  //     schemas: [

  //       {
  //         label: '区域',
  //         field: 'area',
  //         // fieldLabel: 'basCus.name',
  //         component: 'TreeSelect',
  //         componentProps: {
  //           api: areaTreeData,
  //           canSelectParent: false,
  //           allowClear: true,
  //           immediate: true,
  //         },
  //         itemProps: { validateTrigger: 'blur' },
  //       },
  //     ],
  //   });

  // 重置
  function handleReset() {
    // setFieldsValue(defaultValue.value);
    handleData({});
  }
  // 查询
  async function handleSubmit() {
    let obj = await getFieldsValue();
    handleData(obj);
  }

  async function handleData(params) {
    // const res = await getHospTypeCensus(params);
    let data = props.res.data.map((item) => {
      return {
        value: item.iqty,
        name: item.ctype,
      };
    });
    setOptions({
      tooltip: {
        trigger: 'item',
      },

      series: [
        {
          name: '医疗机构统计',
          type: 'pie',
          radius: '80%',
          center: ['50%', '50%'],
          // color: ['#5ab1ef', '#b6a2de', '#67e0e3', '#2ec7c9'],
          data: data,
          // roseType: 'radius',
          // animationType: 'scale',
          // animationEasing: 'exponentialInOut',
          // animationDelay: function () {
          //   return Math.random() * 400;
          // },
          label: {
            formatter: '{name|{b}} ({time|{c}})',
            rich: {
              time: {
                color: '#999',
              },
            },
          },
        },
      ],
    });
  }
</script>
